package tech.aichan.AiQA.controller;

import cn.hutool.http.HttpStatus;
import tech.aichan.AiQA.common.BaseResponse;
import tech.aichan.AiQA.common.DeleteRequest;
import tech.aichan.AiQA.model.dto.item.ItemAddRequest;
import tech.aichan.AiQA.model.dto.item.ItemQueryRequest;
import tech.aichan.AiQA.model.dto.item.ItemUpdateRequest;
import tech.aichan.AiQA.model.vo.ItemVO;
import tech.aichan.AiQA.service.ItemService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mengyu
 * @date 2024/03/03 10:11
 */
@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {
    @Resource
    private ItemService itemService;

    @PreAuthorize("hasAnyRole('admin','user')")
    @PostMapping("/add")
    public BaseResponse<Boolean> addItem(@Valid @RequestBody ItemAddRequest itemAddRequest){
        String itemName = itemAddRequest.getItemName();
        List<String> tags = itemAddRequest.getTags();
        Double price = itemAddRequest.getPrice();
        itemService.addItem(itemName,tags,price);
        return BaseResponse.success(HttpStatus.HTTP_OK,true);
    }

    @PreAuthorize("hasAnyRole('admin','user')")
    @GetMapping("/get/{id}")
    public BaseResponse<ItemVO> getItemById(@PathVariable("id") Long id){
        return BaseResponse.success(HttpStatus.HTTP_OK,ItemVO.getItemVOByItem(itemService.getById(id)));
    }

    @PreAuthorize("hasAnyRole('admin','user')")
    @PutMapping("/update")
    public BaseResponse<Boolean> updateItem(@Valid @RequestBody ItemUpdateRequest itemUpdateRequest){
        itemService.updateItem(itemUpdateRequest);
        return BaseResponse.success(HttpStatus.HTTP_OK, true);
    }

    @PreAuthorize("hasAnyRole('admin','user')")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteItem(@Valid @RequestBody DeleteRequest deleteRequest){
        itemService.deleteItem(deleteRequest.getId());
        return BaseResponse.success(HttpStatus.HTTP_OK, true);
    }

    @PreAuthorize("hasAnyRole('admin','user')")
    @GetMapping("/get")
    public BaseResponse<Page<ItemVO>> getItem(@Valid @RequestBody ItemQueryRequest itemQueryRequest){
        String all = itemQueryRequest.getAll();
        Integer page = itemQueryRequest.getPage();
        Integer size = itemQueryRequest.getSize();
        Boolean priceAsc = itemQueryRequest.getPriceAsc();
        Boolean dateAsc = itemQueryRequest.getDateAsc();
        return BaseResponse.success(HttpStatus.HTTP_OK,itemService.getItem(all,page,size,priceAsc,dateAsc));
    }
}
